package com.hcinfos.gsoo.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.GsooApplication;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.adapter.SearchResultAdapter;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.config.Urls;
import com.hcinfos.gsoo.enity.City;
import com.hcinfos.gsoo.enity.ViewSuppDetail;
import com.hcinfos.gsoo.enity.ViewSuppSearchExt;
import com.hcinfos.gsoo.https.HttpClientUtil;
import com.hcinfos.gsoo.json.SearchResultJson;
import com.hcinfos.gsoo.util.LogUtil;

public class SearchResultActivity extends Activity implements OnClickListener {

	private TextView agreeTv;
	private TextView reg_tv;
	private ImageButton left_ib;
	private TextView center_tv;
	private Context context;

	private ListView result_listView;
	private List<ViewSuppDetail> list;
	private SearchResultAdapter adapter;
	private int totalNums = 0;

	private final int DATA_EMPTY = 0x1120;// 空数据
	private final int DATA_GET_SUCCESS = DATA_EMPTY + 1;
	private final int DATA_GET_ERROR = DATA_EMPTY + 2;// 服务器请求数据失败
	private final int DATA_GET_ALL = DATA_EMPTY + 3;// 加载完所有的数据

	private RelativeLayout progress_layout;
	private View moreView;
	private ProgressBar pageBar;
	private TextView loading_tv;
	private RelativeLayout load_rl;
	private String cityId = null;
	private RelativeLayout data_layout;
	private LinearLayout loading_dialog_content_layout;
	private RelativeLayout loading_staus_rl;
	private TextView status_tv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int width = this.getWindowManager().getDefaultDisplay().getWidth();
		Common.PROPORTION = width / 320f;
		Common.WIDTH = width;
		list = new ArrayList<ViewSuppDetail>();
		context = this;
		setContentView(R.layout.activity_search_result);
		adapter = new SearchResultAdapter(context,list);
		initRes();
		initData();
	}

	private void initRes() {
		left_ib = (ImageButton) findViewById(R.id.left_ib);
		reg_tv = (TextView) findViewById(R.id.reg_tv);
		left_ib.setOnClickListener(this);
		reg_tv.setOnClickListener(this);
		reg_tv.setVisibility(View.GONE);
		center_tv = (TextView) findViewById(R.id.center_tv);
		center_tv.setText("搜索结果");
		result_listView = (ListView) findViewById(R.id.result_listView);
		moreView = getLayoutInflater().inflate(R.layout.loader_more, null);
		loading_tv = (TextView) moreView.findViewById(R.id.load_tv);
		load_rl = (RelativeLayout) moreView.findViewById(R.id.load_rl);
		load_rl.setOnClickListener(this);
		load_rl.setClickable(false);
		pageBar = (ProgressBar) moreView.findViewById(R.id.pb);
		result_listView.addFooterView(moreView);
		result_listView.setAdapter(adapter);
		result_listView.setOnItemClickListener(onItemClickListener);
		data_layout = (RelativeLayout) findViewById(R.id.data_layout);
		data_layout.setVisibility(View.GONE);
		progress_layout = (RelativeLayout) findViewById(R.id.progress_layout);
		progress_layout.setOnClickListener(this);
		progress_layout.setVisibility(View.VISIBLE);
		loading_dialog_content_layout = (LinearLayout) findViewById(R.id.loading_dialog_content_layout);
		loading_staus_rl = (RelativeLayout) findViewById(R.id.loading_staus_rl);
		loading_staus_rl.setVisibility(View.GONE);// 内容提示
		loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
		status_tv = (TextView) findViewById(R.id.status_tv);
	}

	private int page = 1;
	private int rows = 10;
	private String keyword;

	private void initData() {
		keyword = getIntent().getStringExtra("keyword");
		center_tv.setText(keyword);
		LogUtil.d("wxf", "keyword:" + keyword);
		obtainInfoByKeyword();
	}

	@Override
	protected void onResume() {
		super.onResume();
		City city = GsooApplication.currentCity;
		if (city != null)
			cityId = city.getCityId();
	}

	OnItemClickListener onItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			if (position >= list.size()) {
				return;
			}
			
			Intent intent = new Intent(SearchResultActivity.this,
					ShopDetailActivity.class);
			intent.putExtra("shopDetail", list.get(position));
			startActivity(intent);
		}
	};

	Handler handler = new Handler() {

		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case DATA_EMPTY:
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				status_tv.setText("暂无数据!");
				progress_layout.setClickable(false);
				break;
			case DATA_GET_SUCCESS:
				data_layout.setVisibility(View.VISIBLE);
				pageBar.setVisibility(View.GONE);
				progress_layout.setVisibility(View.GONE);
				adapter.setDataChange(list);
				LogUtil.d("wxf", "totalNums:" + totalNums);
				if (totalNums > rows) {
					loading_tv.setVisibility(View.VISIBLE);
					pageBar.setVisibility(View.GONE);
				} else {
					result_listView.removeFooterView(moreView);
				}
				break;
			case DATA_GET_ERROR:
				loading_staus_rl.setVisibility(View.VISIBLE);
				loading_dialog_content_layout.setVisibility(View.GONE);
				// progress_layout.setVisibility(View.GONE);
				break;
			case DATA_GET_ALL:
				result_listView.removeFooterView(moreView);
				break;

			}
		};
	};

	private void obtainInfoByKeyword() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				try {
					String result = HttpClientUtil.getFromWebByHttpClient(context,
							Urls.SEARCH_KEYWORD + "page=" + page + "&rows="
									+ rows + "&k=" + keyword);
					if (result == null) {
						Message msg = handler.obtainMessage();
						msg.what = DATA_GET_ERROR;
						handler.sendMessage(msg);
					} else {
						List<ViewSuppSearchExt> resultList = SearchResultJson
								.getSearchListByJson(result);
						totalNums = SearchResultJson.totalNums;
						if (resultList.size() == 0) {
							if (totalNums > 0 && totalNums > rows) {
								Message msg = handler.obtainMessage();
								msg.what = DATA_GET_ALL;
								handler.sendMessage(msg);
							} else {
								Message msg = handler.obtainMessage();
								msg.what = DATA_EMPTY;
								handler.sendMessage(msg);
							}
						} else {
							load_rl.setClickable(true);
							list.addAll(resultList);
							Message msg = handler.obtainMessage();
							msg.what = DATA_GET_SUCCESS;
							handler.sendMessage(msg);
						}
					}
					LogUtil.d("wxf", "result:" + result);
				} catch (Exception e) {
					Message msg = handler.obtainMessage();
					msg.what = DATA_EMPTY;
					handler.sendMessage(msg);
					LogUtil.d("wxf", "e" + e.toString());
				}
			}
		}).start();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_ib:
			finish();
			break;
		case R.id.load_rl:
			page += 1;
			pageBar.setVisibility(View.VISIBLE);
			obtainInfoByKeyword();
			break;
		case R.id.progress_layout:
			loading_staus_rl.setVisibility(View.GONE);// 内容提示
			loading_dialog_content_layout.setVisibility(View.VISIBLE);// 加载条
			obtainInfoByKeyword();
			break;
		}
	}

}
